const db = require('../db/db')
const {paging} = require('../utils/paging')
module.exports = {
    // 获取工资条
    GETSALARY: async (ctx, next) => {
        const {startPage,endPage} = await paging(ctx)
        const {belongToShop} = ctx.request.query
         try {
            let [res] = await db.promise().query('select * from salary where belongToShop=? order by createTime desc limit ?,?',
            let [total] = await db.promise().query('select * from salary where belongToShop=?')
            [belongToShop,startPage,endPage])
            ctx.body = {
                code:200,
                msg:'获取工资条成功',
                salaryData:res,
                total:total.length
            }
         } catch (error) {
            ctx.body = {
                code:500,
                msg:'获取工资条失败'+error
            }
         } 
    },
    // 添加工资条
    ADDSALARY: async (ctx, next) => {
        const {id,salaryName,salaryAge,department,position,salary,foodPerk,belongToShop} = ctx.request.body
        const createTime = new Date()
        const month = createTime.getMonth() + 1
       try {
        let [res] = await db.promise()
        .query('insert into salary(id,salaryName,salaryAge,department,position,salary,foodPerk,month,belongToShop,createTime) values(?,?,?,?,?,?,?,?,?,?)',
        [id,salaryName,salaryAge,department,position,salary,foodPerk,month,belongToShop,createTime])
        if(res.affectedRows > 0){
            ctx.body = {
                code:200,
                msg:'添加工资条成功'
            }
           }
       } catch (error) {
            ctx.body = {
                code:201,
                msg:'添加工资条失败' +error
            }
       }
    },
    // 更新工资条
    UPDATESALARY: async (ctx, next) => {
        const {salaryName,salaryAge,department,position,salary,foodPerk,workDay,notWorkDay,id} =ctx.request.body
        try {
            let res =await db.promise().query('update salary set salaryName=?,salaryAge=?,department=?,position=?,salary=?,foodPerk=?,workDay=?,notWorkDay=? where id=?',
            [salaryName,salaryAge,department,position,salary,foodPerk,workDay,notWorkDay,id])
            if(res.affectedRows !== 0){
                ctx.body = {
                    code:200,
                    msg:"更新工资条成功"
                }
            }else{
                ctx.body = {
                    code:500,
                    msg:"未找到该员工工资条"
                }
            }
        } catch (error) {
            ctx.body = {
                code:500,
                msg:"更新失败"+error
            }
        }
    },
    // 删除工资条
    DELSALARY: async (ctx, next) => {
        const {id} = ctx.request.body
        try {
           let [res] = await db.promise().query('delete from salary where id=?',[id])
           if(res.affectedRows > 0){
            ctx.body = {
                code:200,
                msg:'操作成功'
            }
           }
        } catch (error) {
            ctx.body = {
                code:201,
                msg:'操作失败'
            }
        }
    },
    // 搜索工资条
    SEARCHSALARY: async (ctx, next) => {
        const {keyWord, belongToShop} = ctx.request.body
        try {
            let [nameRes] = await db.promise().query('select * from salary where salaryName=? and belongToShop=?' ,[keyWord, belongToShop])
            let [iphoneRes] = await db.promise().query('select * from salary where department=? and belongToShop=?' ,[keyWord, belongToShop])
            if(nameRes.length > 0){
                ctx.body = {
                    code:200,
                    msg:"搜索成功",
                    salary:nameRes
                }
            }else if(iphoneRes.length>0){
                ctx.body = {
                    code:200,
                    msg:"搜索成功",
                    salary:iphoneRes
                }
            }else{
                ctx.body = {
                    code:200,
                    msg:"没有该员工工资条",
                }
            }
        } catch (error) {
            ctx.body = {
                code:500,
                msg:"搜索失败" + error
            }
        }
    },
    UPDATECMIS: async (ctx, next) => {
        const {salaryName,foodPerk,goodsCmis,packageCmis,baseCmis,fullDay,workDay,notWorkDay,id} =ctx.request.body
        try {
            let res =await db.promise().query('update salary set salaryName=?,foodPerk=?,goodsCmis=?,packageCmis=?,baseCmis=?,fullDay=?,workDay=?,notWorkDay=? where id=?',
            [salaryName,foodPerk,goodsCmis,packageCmis,baseCmis,fullDay,workDay,notWorkDay,id])
            if(res.affectedRows !== 0){
                ctx.body = {
                    code:200,
                    msg:"更新工资提成成功"
                }
            }else{
                ctx.body = {
                    code:500,
                    msg:"未找到该员工工资条"
                }
            }
        } catch (error) {
            ctx.body = {
                code:500,
                msg:"更新失败"+error
            }
        }
    },
}